package com.cn.ant.modules.score.service;

import com.cn.ant.common.persistence.Page;
import com.cn.ant.common.service.BaseService;
import com.cn.ant.modules.score.dao.TwfInfoDao;
import com.cn.ant.modules.score.entity.TwfInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
/**
 * 同位分Service
 * @author AntDream
 * @version 2015-08-19
 */
@Service
@Transactional(readOnly = true)
public class TwfInfoService extends BaseService {

    @Autowired
	private TwfInfoDao twfInfoDao;

	public TwfInfo get(String id) {
		return twfInfoDao.get(id);
	}
	
	public List<TwfInfo> findList(TwfInfo twfInfo) {
		return twfInfoDao.findList(twfInfo);
	}
	
	public Page<TwfInfo> findPage(Page<TwfInfo> page, TwfInfo twfInfo) {
	    twfInfo.setPage(page);
        page.setList(twfInfoDao.findList(twfInfo));
        return page;
	}
	
	@Transactional(readOnly = false)
	public void save(TwfInfo twfInfo) {
	    if (twfInfo.getIsNewRecord()) {
            twfInfo.preInsert();
            twfInfoDao.insert(twfInfo);
        } else {
            twfInfo.preUpdate();
            twfInfoDao.update(twfInfo);
        }
	}

    @Transactional(readOnly = false)
	public void batchSave(List<TwfInfo> list) {
        List<TwfInfo> temp = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            if ((i % 200 == 0 && i > 0) || i == list.size() - 1) {
                twfInfoDao.batchInsert(temp);
                temp = new ArrayList<>();
            }
            list.get(i).preInsert();
            temp.add(list.get(i));
        }

    }

    @Transactional(readOnly = false)
	public void delete(TwfInfo twfInfo) {
		twfInfoDao.delete(twfInfo);
	}
	
}